﻿using System;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Windows.Data;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace MSFforAgileSoftwareDevelopmentv50.Converters
{
    class ReproStepsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var workItem = value as WorkItem;
            string fieldName = "Repro Steps";
            if (workItem != null)
                if(workItem.Fields[fieldName].Value != null)
                {
                    string reproStepsValue = workItem.Fields[fieldName].Value.ToString();
                    return HtmlRemoval.StripTagsRegex(reproStepsValue);
                }


            return "-";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}



/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
    /// <summary>
    /// Remove HTML from string with Regex.
    /// </summary>
    public static string StripTagsRegex(string source)
    {
        string sourceString;
        sourceString = Regex.Replace(source, "</P>", "\r\n");
        sourceString = Regex.Replace(sourceString, "<BR>", "\r\n");
        sourceString = Regex.Replace(sourceString, "<.*?>", string.Empty);
        sourceString = Regex.Replace(sourceString, "&nbsp;", " ");
	    return sourceString;
    }

    
}